﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using AssetBundles;

public class ResMgr : UnitySingleton<ResMgr> {
    public override void Awake() {
        base.Awake();
        this.gameObject.AddComponent<AssetBundleManager>();
    }

    public T GetAssetCache<T>(string name) where T : UnityEngine.Object
    {
#if UNITY_EDITOR
        if (AssetBundleConfig.IsEditorMode)
        {
            string path = AssetBundleUtility.PackagePathToAssetsPath(name);
            // Debug.Log(path);
            UnityEngine.Object target = UnityEditor.AssetDatabase.LoadAssetAtPath<T>(path);
            return target as T;
        }
#endif
        return AssetBundleManager.Instance.GetAssetCache(name) as T;
    }

    public void LoadAssetBundleAsync(string assetbundleName, Action end_func)
    {
        this.StartCoroutine(this.IE_LoadAssetBundleAsync(assetbundleName, end_func));
    }

    public IEnumerator IE_LoadAssetBundleAsync(string assetbundleName, Action end_func)
    {
        var loader = AssetBundleManager.Instance.LoadAssetBundleAsync(assetbundleName);
        yield return loader;
        loader.Dispose();

        end_func();
    }
}
